<#--
List of variables to be passed for this template:
        +- rootPackage
        +- applicationName
        +- classNames
        +- directory
        +- Entities: ArrayList of EntityClass.getAsMap()

            |
            +- imports: imports
            |
            +- name: The name of the class.
            |    |
            |    +- single
            |    +- plural
            |
            +- idColumn: the id column
            |    |
            |    +- field
            |    |    |
            |    |    +- name
            |    |    +- type
            |    |
            |    +- getter
            |    +- setter
            |
            +- columns: ArrayList of columns
            |    |
            |    +- field
            |    +- getter
            |    +- setter

-->
<@pp.dropOutputFile />
<#list Entities as entity>
<@pp.changeOutputFile name="/development/${directory}/apis/rest/resources/${entity.name.single}Resource.java" />

package ${rootPackage}.${applicationName}.apis.rest.resources;

import org.restlet.data.MediaType;
import org.restlet.data.Status;
import org.restlet.representation.Representation;
import org.restlet.resource.Delete;
import org.restlet.resource.Get;
import org.restlet.resource.Put;
import org.restlet.resource.ResourceException;
import org.restlet.resource.ServerResource;

import ${rootPackage}.${applicationName}.apis.models.${entity.name.plural};
import ${rootPackage}.${applicationName}.apis.rest.DiagRestRepresentation;
import ${rootPackage}.${applicationName}.apis.rest.converters.${entity.name.single}Converter;
import ${rootPackage}.${applicationName}.models.${entity.name.single};

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.json.JettisonMappedXmlDriver;

public class ${entity.name.single}Resource extends ServerResource {

	private ${entity.name.single} entity;
	private ${entity.name.plural} controller = new ${entity.name.plural}();

	@Override
	protected void doInit() throws ResourceException {

		String id = (String) getRequestAttributes().get("id");
		this.entity = controller.find(id);
	}

	@Get("json")
	public Representation getJson() {
		return new DiagRestRepresentation<${entity.name.single}Converter>(
				MediaType.APPLICATION_JSON, new ${entity.name.single}Converter(this.entity));
	}

	@Put("json")
	public Representation Update(String json) throws ResourceException {

		XStream xstream = new XStream(new JettisonMappedXmlDriver());
		xstream.processAnnotations(${entity.name.single}Converter.class);
		${entity.name.single}Converter converter = (${entity.name.single}Converter) xstream.fromXML(json);

		if (!converter.${entity.idColumn.getter}().equals(this.entity.${entity.idColumn.getter}()))
			throw new ResourceException(Status.CLIENT_ERROR_CONFLICT);

                <#list entity.columns as column>
		if (converter.${column.getter}() != null)
			this.entity.${column.setter}(converter.${column.getter}());
                </#list>

		controller.merge(entity);
		return getJson();
	}

	@Delete
	public void Delete() {
		controller.remove(entity);
	}
}

</#list>
